//
//  BYJIphoneViewController.m
//  BLDMusics
//
//  Created by 白云杰 on 16/3/28.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "BYJIphoneViewController.h"

@interface BYJIphoneViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UITextField *iphoneNumberTextField;
@property (nonatomic, retain) UITextField *passwordTextField;

@end

@implementation BYJIphoneViewController

- (void)viewWillAppear:(BOOL)animated {
    [PlayViewController sharePlayViewBar].hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createControls];
}

- (void)createControls {
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGTH - 64) / 4)];
    imgView.backgroundColor = [UIColor cyanColor];
    imgView.image = [UIImage imageNamed:@"iphone.jpg"];
    [self.view addSubview:imgView];
    
    self.iphoneNumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, (SCREEN_HEIGTH - 64) / 4, SCREEN_WIDTH, 30)];
    _iphoneNumberTextField.delegate = self;
    _iphoneNumberTextField.borderStyle = UITextBorderStyleRoundedRect;
    _iphoneNumberTextField.clearButtonMode = UITextFieldViewModeAlways;
    _iphoneNumberTextField.returnKeyType=UIReturnKeyDone;
    _iphoneNumberTextField.placeholder = @"手机号/邮箱";
    [self.view addSubview:_iphoneNumberTextField];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, (SCREEN_HEIGTH - 64) / 4 + 30, SCREEN_WIDTH, 30)];
    _passwordTextField.delegate = self;
    _passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    _passwordTextField.clearButtonMode = UITextFieldViewModeAlways;
    _passwordTextField.placeholder = @"密码";
    _passwordTextField.keyboardType = UIKeyboardAppearanceAlert;
    _passwordTextField.returnKeyType = UIReturnKeyGo;
    [self.view addSubview:_passwordTextField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(50, (SCREEN_HEIGTH - 64) / 4 + 70, SCREEN_WIDTH - 100, 30);
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.backgroundColor = [UIColor cyanColor];
    [button setTitle:@"登陆" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(50, (SCREEN_HEIGTH - 64) / 4 + 110, 50, 30);
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];
    
    UIButton *forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetButton.frame = CGRectMake(SCREEN_WIDTH - 150, (SCREEN_HEIGTH - 64) / 4 + 110, 100, 30);
    [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(forgetButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetButton];
}

- (void)buttonAction:(UIButton *)button {
    
    [BmobUser loginWithUsernameInBackground:_iphoneNumberTextField.text password:_passwordTextField.text block:^(BmobUser *user, NSError *error) {
        if (error) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"账号或密码错误" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:alertAction1];
            [self presentViewController:alert animated:YES completion:nil];
        } else {
//            NSLog(@"登陆成功");
            RootViewController *VC = [[RootViewController alloc] init];
            [self.navigationController pushViewController:VC animated:YES];
        }
    }];
}

- (void)registerButtonAction:(UIButton *)button {
    
    BYJRegisterViewController *VC = [[BYJRegisterViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)forgetButtonAction:(UIButton *)button {
    
    BYJForgetViewController *VC = [[BYJForgetViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    // 取消第一响应者
    [textField resignFirstResponder];
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
